﻿using Demo.Identity.Permissions;

namespace Demo.Permissions;

public static class CusPermissionExtensions
{
    /// <summary>
    /// 注册自定义权限
    /// </summary>
    public static void UseCusPermissions<T>(this IApplicationBuilder app, string serviceName)
    {
        app.RegistPermissions<T>(serviceName);
        app.UseMiddleware<CusPermissionMiddleware>();
    }

    /// <summary>
    /// 注册权限
    /// </summary>
    /// <param name="app"></param>
    /// <param name="serviceName">服务名称</param>
    /// <typeparam name="T"></typeparam>
    private static async Task RegistPermissions<T>(this IApplicationBuilder app, string serviceName)
    {
        var service = app.ApplicationServices.GetService<ISysPermissionAppService>();
        var permissions = PermissionRegistor.GetPermissions<T>(serviceName);
        await service.RegistPermission(serviceName, permissions);
    }
}